package org.example.lotterysystem.service.impl;

import org.example.lotterysystem.controller.param.CreatePrizeParam;
import org.example.lotterysystem.controller.param.PageParam;
import org.example.lotterysystem.dao.dataobject.PrizeDO;
import org.example.lotterysystem.dao.mapper.PrizeMapper;
import org.example.lotterysystem.service.PictureService;
import org.example.lotterysystem.service.PrizedService;
import org.example.lotterysystem.service.dto.PageListDTO;
import org.example.lotterysystem.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class PrizedServiceImpl implements PrizedService {
    @Autowired
    private PrizeMapper prizeMapper;
    @Autowired
    private PictureService pictureService;

    /**
     * 查询奖品数据
     * @param param
     * @return
     */
    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
        //查询总量
        int count = prizeMapper.count();
        List<PrizeDTO> prizeDTOList=new ArrayList<>();
        List<PrizeDO> prizeDOList = prizeMapper.selectPrizeList(param.offset(), param.getPageSize());
        for(PrizeDO prizeDo:prizeDOList){
            PrizeDTO prizeDTO=new PrizeDTO();
            prizeDTO.setPrizeId(prizeDo.getId());
            prizeDTO.setName(prizeDo.getName());
            prizeDTO.setDescription(prizeDo.getDescription());
            prizeDTO.setImageUrl(prizeDo.getImageUrl());
            prizeDTO.setPrice(prizeDo.getPrice());
            prizeDTOList.add(prizeDTO);
        }

        return new PageListDTO<>(count,prizeDTOList);
    }

    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {
        String fileName = pictureService.savePicture(picFile);
        PrizeDO prizeDO=new PrizeDO();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setPrice(param.getPrice());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setImageUrl(fileName);
        long insert = prizeMapper.insert(prizeDO);
        return insert;
    }


}
